/*
* Licensed under the Apache License, Version 2.0 (the "License"):
*    http://www.apache.org/licenses/LICENSE-2.0
*/
package {

import com.google.maps.LatLng;
import com.google.maps.LatLngBounds;
import com.google.maps.MapEvent;
import com.google.maps.PaneId;
import com.google.maps.interfaces.IMap;
import com.google.maps.interfaces.IPane;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.overlays.OverlayBase;
import com.google.maps.overlays.Polygon;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.utils.Timer;

public class CloudOverlay extends OverlayBase {
  private var markers:Array;
  private var cloud:Shape;
  private var map:IMap;
  private var sparklesTimer:Timer;
  private var sparkles:Array;
  private var sparkleLatLngs:Array;
  
  [Bindable] private var mc:MovieClip;
  public function CloudOverlay(markers:Array) {
    super();
    this.markers = markers;
    trace(this.markers);
    trace(this.markers.length);
    cloud = new Shape();
    sparkles = [];
    sparkleLatLngs = [];
    
    this.addEventListener(MapEvent.OVERLAY_ADDED, onOverlayAdded);
    this.addEventListener(MapEvent.OVERLAY_REMOVED, onOverlayRemoved);
  }

  public override function getDefaultPane(map:IMap):IPane {
  	  this.map = map;
      return map.getPaneManager().getPaneById(PaneId.PANE_OVERLAYS);
  }
  
  private function onOverlayAdded(event:MapEvent):void {
  	positionOverlay(false);
  }

  private function onOverlayRemoved(event:MapEvent):void {
  	cloud.graphics.clear();
  }

  public override function positionOverlay(zoomChanged:Boolean):void {
  	var point:Point;
  	var i:int;
  	
  	cloud.graphics.clear();
  	if (map.getZoom() < 5 && this.markers.length >= 2) {
	  	cloud.graphics.clear();
	  	cloud.graphics.lineStyle(1, 0xFFFFFF, 1.0);
	  	point = this.pane.fromLatLngToPaneCoords(this.markers[this.markers.length - 1].getLatLng(), true);
	    cloud.graphics.moveTo(point.x, point.y);
	  	for (i = (this.markers.length - 2); i > -1; i--) {
	  		var diff:int = this.markers.length - i;
	  		cloud.graphics.lineStyle(30 - (diff*4), 0xFFFFFF, 1.0 - (diff*.1));
	  		point = this.pane.fromLatLngToPaneCoords(this.markers[i].getLatLng(), true);
	  		cloud.graphics.lineTo(point.x, point.y);
	    }
	    cloud.filters = [new BlurFilter(30, 30, 2)];
	    addChild(cloud);
    }
    if (sparkles.length == 0) {
     var currentRadius:Number = 2;
  	 for (i = 1; i < this.markers.length; i++) {
        currentRadius += 3;
        createSparkles(this.markers[i].getLatLng(), currentRadius);
     }
    } else {
      for (i = 1; i < this.sparkles.length; i++) {
    	point = this.pane.fromLatLngToPaneCoords(sparkleLatLngs[i]);
    	sparkles[i].x = point.x;
        sparkles[i].y = point.y;
      }
    }
  }
  
  private function createSparkles(latlng:LatLng, radius:Number):void {
  	radius = radius/2;
  	var lat:Number = latlng.lat();
  	var lng:Number = latlng.lng();
  	var d2r:Number = Math.PI/180;
    var r2d:Number = 180/Math.PI;
    var Clng:Number = radius/Math.cos(lat*d2r); 
    var circleLatLngs:Array = []; 
    for (var i:uint = 0; i < 33; i++) { 
      var theta:Number = Math.PI * (i/16); 
      var circleLat:Number = lat + (radius * Math.sin(theta)); 
      var circleLng:Number = lng + (Clng * Math.cos(theta)); 
    circleLatLngs.push(new LatLng(circleLat, circleLng)); 
    }
    var highlightCircle:Polygon = new Polygon(circleLatLngs);
    
    var bounds:LatLngBounds = highlightCircle.getLatLngBounds();
    var southWest:LatLng = bounds.getSouthWest();
    var northEast:LatLng = bounds.getNorthEast();
    var lngSpan:Number = northEast.lng() - southWest.lng();
    var latSpan:Number = northEast.lat() - southWest.lat();
    for (var i:uint = 0; i < Math.floor(radius/4); i++) {
    	  if(lngSpan < - 100) {
    	  	lngSpan = 360 + lngSpan;
    	  } 
          var latlng:LatLng = new LatLng(southWest.lat() + latSpan * Math.random(),
                                  southWest.lng() + lngSpan * Math.random());
           sparkleLatLngs.push(latlng);
           var me = this;
           var loader:Loader = new Loader();
           loader.contentLoaderInfo.addEventListener(Event.INIT, function(event:Event):void {
           	var point:Point = me.pane.fromLatLngToPaneCoords(latlng);
           	loader.x = point.x;
           	loader.y = point.y;
           	loader.width = Math.floor(radius*2);
           	loader.height = Math.floor(radius*2);
            me.addChild(loader);
            var mc:MovieClip = event.target.content as MovieClip;
            mc.stop();
            mc.gotoAndPlay(Math.round(Math.random() * (10)));
            sparkles.push(loader);
           });
           loader.load(new URLRequest("sparkle.swf"));
      }
  }
}
}